import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ViewController, LoadingController, ToastController} from 'ionic-angular';

import {UserService} from "../../providers/UserService";
import {UserInfo} from "../../model/UserInfo";

import {StorageService} from "../../providers/StorageService";
import {STATUS_SUCCESS, USERFACE_PATH} from "../../providers/Constants";

import {HeadfacePage} from "../headface/headface";
import {BaseUI} from "../../common/baseui";
import {JiGuanService} from "../../providers/JiGuanService";

@IonicPage()
@Component({
  selector: 'page-user-info',
  templateUrl: 'user-info.html',
})
export class UserInfoPage extends BaseUI {

  errorMessage: any;
  userFaceImg: String;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public userService: UserService,
              public userInfo: UserInfo,
              public  viewCtrl: ViewController,
              public  storageService: StorageService,
              public loadingCtrl: LoadingController,
              public toastCtrl: ToastController,
              public jiGuanService: JiGuanService) {
    super();
  }


  ionViewDidEnter() {
    this.loadUserInfoPage();
  }

  loadUserInfoPage() {
    let loader = this.loadingCtrl.create({content: "正在获取数据...",});
    loader.present();
    // super.showLoading(this.loadingCtrl,"正在获取数据...");
    var id = this.storageService.read<UserInfo>('UserInfo').id;
    this.userService.getUserInfo(id).subscribe(
      res => {
        if (res["status"] == 0) {
          this.userInfo = res["data"];
          this.storageService.write('UserInfo', res["data"]);//更新本地
          this.userFaceImg = USERFACE_PATH + "/" + this.userInfo.userFace;
          console.log(this.userInfo);
          loader.dismiss();
        } else {
          loader.dismiss();
          super.showToast(this.toastCtrl,"获取用户信息失败",'bottom');
        }

      }, error => {
        this.errorMessage = error;
        alert(this.errorMessage);
        loader.dismiss();
        super.showToast(this.toastCtrl, "网络请求失败", 'middle');

      });
  };

  //注销
  logout() {
     this.jiGuanService.logout();
     this.storageService.remove('UserInfo');
     this.viewCtrl.dismiss();
  }

  gotoHeadface() {
    this.navCtrl.push(HeadfacePage);
    parent.onload;
  }

  //更新信息
  updateInfo() {
    let loader = this.loadingCtrl.create({
      content: "正在提交...",
    });
    loader.present();
    this.userService.updatInfo(this.userInfo).then(res => {
      if (res["status"] == STATUS_SUCCESS) {
        this.jiGuanService.updateMyInfo(this.userInfo.nickname).then(() => {
          this.toastCtrl.create({message: res['msg'], duration: 3000}).present();
          loader.dismiss();
          this.storageService.write('UserInfo', res["data"]);
          this.viewCtrl.dismiss();
        }).catch((error) => {
          loader.dismiss();
          super.showToast(this.toastCtrl, JSON.stringify(error), 'bottom');
        })
      } else {
        loader.dismiss();
        super.showToast(this.toastCtrl, res["msg"], 'bottom');
      }

    });
  }

}
